# 安装pythong
# pip install pyautogui opencv-python numpy pygetwindow Pillow
import pyautogui
import cv2
import time
import numpy as np
import pygetwindow as gw
from PIL import Image

# 等待游戏加载完毕
time.sleep(3)

def push_button(acction):
    pyautogui.keyDown(acction)
    pyautogui.keyUp(acction)
    time.sleep(0.3)

def case_png(img_path):
    # 截取当前整个屏幕
    screenshot = pyautogui.screenshot()
    screenshot.save('screenshot.png')  # 保存截图到本地

    # 使用Pillow打开截图
    image = Image.open('screenshot.png')

    # 将Pillow图像转换为OpenCV格式
    cv_image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)

    # 加载要查找的图像模板
    template = cv2.imread(img_path)

    # 使用模板匹配方法查找图像位置
    result = cv2.matchTemplate(cv_image, template, cv2.TM_CCOEFF_NORMED)
    threshold = 0.8  # 设置匹配阈值

    loc = np.where(result >= threshold)
    if len(loc[0]) == 0:
        # 未找到图像模板
        return False
    else:
        return True
        
def move_to_senlinju():
    # 使用传真移动到森林局
    # 打开菜单
    push_button('esc')  # 按下Esc键
    # 点击物品
    push_button('enter')
    # 选择特殊物品栏
    push_button('up')
    # 点击特殊物品
    push_button('enter')
    # 点击传真装置
    push_button('enter')
    # 点击使用
    push_button('enter')
    # 选择森林局
    push_button('right')
    push_button('right')
    push_button('down')
    # 点击森林局
    push_button('enter')
    
def check_fight():
    # 检查是否遇敌
    if case_png('yudi.png') == True:
        return True
    else:
        return False

def exit_fight():
    # 退出战斗
    push_button('esc')  # 按下Esc键
    push_button('down')
    push_button('down')
    push_button('right')
    push_button('enter')
    


def move_to_up_level_point():
    print(f"移动到刷级点")
    push_button('right')
    push_button('up')
    push_button('up')
    push_button('right')
    push_button('up')
    push_button('up')
    push_button('up')
    push_button('left')
    push_button('up')
    push_button('left')
    push_button('left')
    push_button('up')
    push_button('up')
    push_button('up')
    push_button('up')
    push_button('left')
    push_button('up')
    push_button('up')
    push_button('left')
    push_button('up')
    push_button('up')
    push_button('right')
    push_button('up')
    push_button('up')
    push_button('left')
    push_button('up')
    push_button('up')
    push_button('left')
    push_button('left')
    push_button('left')
    push_button('up')
    push_button('up')
    push_button('left')
    push_button('left')
    push_button('left')
    push_button('left')
    push_button('left')
    push_button('left')
    push_button('left')
    push_button('left')
    push_button('up')
    push_button('up')
    push_button('up')
    push_button('left')
    push_button('left')
    push_button('left')
    push_button('left')
    push_button('up')
    push_button('left')
    push_button('up')
    push_button('up')
    push_button('left')
    push_button('up')
    push_button('up')
    push_button('up')
    push_button('up')
    push_button('up')
    push_button('up')
    push_button('up')
    push_button('up')
    push_button('up')
    push_button('up')
    push_button('up')
    push_button('up')

# 获取所有窗口
windows = gw.getAllWindows()

# 找到当前活动窗口
for window in windows:
    if window.isActive:
        print(f"当前活动窗口的标题是: {window.title}")
        break

# 活动窗口的标题 重装机兵Lurk正式版1.15（QQ群308276525@夜灵）

# # 移动到森林局
# move_to_senlinju()
# # 移动到刷级点
# if case_png('senlinju_start.png') == True:
#     move_to_up_level_point()

while True:

    # 截取整个屏幕
    screenshot = pyautogui.screenshot()
    screenshot.save('screenshot.png')  # 保存截图到本地

    # 使用Pillow打开截图
    image = Image.open('screenshot.png')

    # 将Pillow图像转换为OpenCV格式
    cv_image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)

    # 定义模板和对应的操作
    templates = {
        'fight_end.png': 'fight_end',
        'left_end.png': 'right',
        'meet1.png': 'start',
        'meet2.png': 'start',
        'fight.png': 'fight',
        'fighting.png': 'fighting',
        'get_tool.png': 'fight_end',
        'update1.png': 'fight_end',
        'update2.png': 'fight_end',
    }

    # 设置匹配阈值
    threshold = 0.8  
    
    # 遍历每个模板进行匹配
    for template_name, action in templates.items():
        template = cv2.imread(template_name)
        result = cv2.matchTemplate(cv_image, template, cv2.TM_CCOEFF_NORMED)
        loc = np.where(result >= threshold)

        if len(loc[0]) > 0:
            # 找到图像模板，执行对应的操作
            print(f"找到{template_name}，执行{action}操作")
            if action == 'right':
                pyautogui.keyDown('right')  # 按下向右键
                time.sleep(2)
                pyautogui.keyUp('right')    # 松开向右键
            elif action == 'start':
                pyautogui.keyDown('space')  # 按下向右键
                time.sleep(0.2)
                pyautogui.keyUp('space')    # 松开向右键
                time.sleep(0.5)
            elif action == 'fight':
                pyautogui.keyDown('esc')  # 按下Esc键
                pyautogui.keyUp('esc')    # 松开Esc键
                time.sleep(0.3)
                print('开始战斗')

                pyautogui.keyDown('right')
                pyautogui.keyUp('right')
                time.sleep(0.3)
                pyautogui.keyDown('enter')
                pyautogui.keyUp('enter')
                time.sleep(0.3)
                pyautogui.keyDown('enter')
                pyautogui.keyUp('enter')
                time.sleep(0.3)
                
                pyautogui.keyDown('right')
                pyautogui.keyUp('right')
                time.sleep(0.3)
                pyautogui.keyDown('enter')
                pyautogui.keyUp('enter')
                time.sleep(0.3)
                pyautogui.keyDown('enter')
                pyautogui.keyUp('enter')
                time.sleep(0.3)
                
                # pyautogui.keyDown('right')
                # pyautogui.keyUp('right')
                # time.sleep(0.3)
                pyautogui.keyDown('enter')
                pyautogui.keyUp('enter')
                time.sleep(0.3)
                pyautogui.keyDown('enter')
                pyautogui.keyUp('enter')
                time.sleep(0.3)
            elif action == 'fighting':
                time.sleep(0.5)
            elif action == 'fight_end':
                pyautogui.keyDown('enter')  # 按下Esc键
                pyautogui.keyUp('enter')    # 松开Esc键
                time.sleep(0.5)
            break

    # 未找到任何图像模板
    if len(loc[0]) == 0:
        print('未到最左边，向左移动')
        # pyautogui.press('left')  # 单次按下向左键
        pyautogui.keyDown('left')  # 按下向左键
        time.sleep(2)
        pyautogui.keyUp('left')    # 松开向左键

    # 等待1秒
    time.sleep(0.5)

# 模拟键盘操作
# pyautogui.press('up')    # 按下向上键
# pyautogui.press('down')  # 按下向下键
# pyautogui.press('left')  # 按下向左键
# pyautogui.press('right') # 按下向右键
# pyautogui.press('A')  # 按下向A键



    # for pt in zip(*loc[::-1]):  # 反转坐标以适应矩形绘图
    #     cv2.rectangle(cv_image, pt, (pt[0] + template.shape[1], pt[1] + template.shape[0]), (0, 255, 255), 2)

    # # 显示结果图像
    # cv2.imshow('Detected', cv_image)
    # cv2.waitKey(0)
    # cv2.destroyAllWindows()

